//
//  DataSingleton.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/31/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "DataSingleton.h"
#import "Global.h"

@implementation DataSingleton

static DataSingleton *_shared;
+ (DataSingleton *)shared
{
    if (_shared == nil)
    {
        _shared = [[DataSingleton alloc] init];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_CHANGE_LANGUAGE object:nil];
        
    }
    
    return _shared;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        NSNumber *isDarkTheme = [[NSUserDefaults standardUserDefaults] objectForKey:@"isDarkTheme"];
        
        if (isDarkTheme)
        {
            _isDarkTheme = [isDarkTheme boolValue];
        }
        else
        {
            _isDarkTheme = YES;
        }
        
        //
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"Language"];
        if (language == nil)
        {
            language = LANGUAGE_VI;
        }
        
        self.language = language;
    }
    
    return self;
}

- (void)setIsDarkTheme:(BOOL)isDarkTheme
{
    _isDarkTheme = isDarkTheme;
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:isDarkTheme] forKey:@"isDarkTheme"];
}

- (void)setLanguage:(NSString *)language
{
    _language = language;
    
    if ([_language isEqualToString:LANGUAGE_EN])
    {
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"vi", nil] forKey:@"AppleLanguages"];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"vi", @"en", nil] forKey:@"AppleLanguages"];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:_language forKey:@"Language"];
}

@end
